package com.coocaa.launcher.framework.controller;

import android.content.Context;

import com.coocaa.launcher.framework.controller.callback.DirectoryChangerListener;
import com.coocaa.launcher.framework.controller.data.FileData;
import com.coocaa.x.framework.utils.FileUtils.FILETYPE;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.Storage;
import com.coocaa.x.framework.utils.Storage.IStorageListener;
import com.coocaa.x.framework.utils.Storage.SDevice;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ExternalStorageDierectController extends BaseController implements IStorageListener {
    private static final String tag = "file";
    private static ExternalStorageDierectController instance = null;
    public static final String BASEPATH = "/mnt";
    private String curPath = "";
    private DirectoryChangerListener mDirectoryChangerListener = null;

    public static ExternalStorageDierectController getInstance() {
        if (instance == null)
            instance = new ExternalStorageDierectController(mContext);
        return instance;
    }

    private ExternalStorageDierectController(Context c) {

    }

    public void setDirectoryChangerListener(DirectoryChangerListener listener) {
        mDirectoryChangerListener = listener;
    }

    @Override
    public void initController() {
        Storage.addListener(this);
    }

    @Override
    public void onDestory() {
        Storage.removeListener(this);
    }

    public List<FileData> getRootDirectory() {
        File rootDir = new File(BASEPATH);
        if (!rootDir.exists()) {
            LogUtils.d(tag, "have no mnt directory...");
            return null;
        }
        return getFileDataList(rootDir);
    }

    public List<FileData> getFileDataList(File baseFile) {
        String[] fileList = baseFile.list();
        List<FileData> list = new ArrayList<FileData>();
        if (fileList != null && fileList.length > 0) {
            for (int i = 0; i < fileList.length; i++) {
                String path = baseFile.getAbsolutePath() + "/" + fileList[i];
                boolean isDirectory = false;
                FILETYPE type = FILETYPE.TEXT;
                File f = new File(path);
                if (f.isDirectory()) {
                    isDirectory = true;
                    type = FILETYPE.FOLD;
                } else {
                    isDirectory = false;
                    type = getFileType(path);
                }
                FileData data = new FileData(fileList[i], path, isDirectory, type);
                LogUtils.d(tag, "name:" + fileList[i]);
                list.add(data);
            }
        }
        return list;
    }

    public FILETYPE getFileType(String path) {
        if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg")) {
            return FILETYPE.PHOTO;
        } else if (path.endsWith(".mp3") || path.endsWith(".wav") || path.endsWith(".aac")) {
            return FILETYPE.MUSIC;
        } else if (path.endsWith(".rmvb") || path.endsWith(".mp4") || path.endsWith(".wmv")
                || path.endsWith(".rmb")) {
            return FILETYPE.MOVIE;
        } else if (path.endsWith(".apk")) {
            return FILETYPE.APK;
        } else {
            return FILETYPE.TEXT;
        }
    }

    public void showFileList(String path) {
        LogUtils.d(tag, "showFileList path:" + path);
        File f = new File(path);
        if (!f.exists()) {
            LogUtils.d(tag, "this path is miss!!!!!");
            return;
        }

        if (!f.isDirectory()) {
            LogUtils.d(tag, "need app to open this file....");
            mDirectoryChangerListener.startApp(f);
        } else {
            LogUtils.d(tag, "show file list!!!!!");
            curPath = path;
            List<FileData> list = getFileDataList(f);
            mDirectoryChangerListener.updateFileList(path, list);
        }
    }

    public void showLastFileList() {
        int index = curPath.lastIndexOf("/");
        String lastPath = curPath.substring(0, index);
        LogUtils.d(tag, "showLastFileList  curPath:" + curPath + " lastPath:" + lastPath);
        showFileList(lastPath);
    }

    public boolean isBasePath() {
        if (curPath.endsWith(BASEPATH))
            return true;
        return false;
    }

    @Override
    public void onStorageMounted(SDevice device) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStorageRemoved(SDevice device) {
        // TODO Auto-generated method stub

    }

}
